﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Comm;
using System.Text;
using Model.RolePopedom;
using System.Collections;

public partial class Web_Admin_Admin_role_add : AdminPageBase
{
    private int id = 0;
    private List<MModulePermmission> modulelist = null;
    private List<Model.TPermission> tperlist = null;
    private BLL.Role rolebll = new BLL.Role();
    private BLL.TPermission perbll = new BLL.TPermission();
    private ModulePermmissionDispose bll = new ModulePermmissionDispose();
    protected void Page_Load(object sender, EventArgs e)
    {
        base.CheckPopedomInfoRedirect(1004, 1);
        link_del.Enabled = base.CheckPopedomInfo(1004, 8);
        but_assave.Enabled = base.CheckPopedomInfo(1004, 2);
        id = base.QueryInt("id");
        if (!IsPostBack)
        {
            loadData();
            loadType();
        }
    }

    /// <summary>
    /// 加载类型
    /// </summary>
    private void loadType()
    {
        modulelist = bll.getList();
        rpList.DataSource = modulelist;
        rpList.DataBind();

        drpClass.DataSource = rolebll.DataTableToList(rolebll.GetList(0, "", "Ordinal asc").Tables[0]);
        drpClass.DataBind();
    }

    /// <summary>
    /// 加载数据
    /// </summary>
    private void loadData()
    {
        if (id > 0)
        {
            but_save.Enabled = base.CheckPopedomInfo(1004, 4);
            drpClass.SelectedValue = id.ToString();
            Model.Role model = rolebll.GetModel(id);
            if (model == null)
            {
                return;
            }
            txt_RoleName.Text = model.RoleName;
            txt_Ordinal.Text = model.Ordinal.ToString();

            tperlist = perbll.DataTableToList(perbll.GetList(0, "RoleID=" + id, "RoleID asc").Tables[0]);
        }
        else
        {
            but_save.Enabled = base.CheckPopedomInfo(1004, 2);
        }
    }

    /// <summary>
    /// 获取指定模块的权限
    /// </summary>
    /// <param name="FormID"></param>
    /// <returns></returns>
    protected string getRoleList(object PermmissionList,object moduleid)
    {
        StringBuilder sb = new StringBuilder("<ul>");
        List<MPermmission> list = PermmissionList as List<MPermmission>;
        if (list != null)
        {
            foreach (MPermmission item in list)
            {
                string chk = "";
                if (tperlist != null)
                {
                    chk = tperlist.Exists(delegate(Model.TPermission model) { return (model.FormID == Convert.ToInt32(moduleid) && (long.Parse(model.Permission.ToString()) & item.ID)==item.ID); }) ? " checked=\"checked\"" : "";
                }
                sb.AppendFormat("<li><table><tr><td><input id=\"checkboxs\" name=\"chk_ids\" type=\"checkbox\"{3} value=\"{1}_{2}\" /></td><td>{0}</td></tr></table></li>", item.Name, moduleid, item.ID, chk);
            }
        }
        sb.Append("</ul>");

        return sb.ToString();
    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void but_save_Click(object sender, EventArgs e)
    {
        save();
    }

    /// <summary>
    /// 存储过程保存
    /// </summary>
    private void save()
    {
        string RoleName = txt_RoleName.Text.Trim();
        int Ordinal = int.Parse(txt_Ordinal.Text.Trim());
        string PermissionID = getPermission();

        Model.Role model = new Model.Role();
        model.ID = id;
        model.RoleName = RoleName;
        model.Ordinal = Ordinal;
        model.AddTime = DateTime.Now;

        int i = rolebll.Add(model, PermissionID);
        if (id > 0)
        {
            base.AddLog(2, "角色ID：" + id);
        }
        else
        {
            base.AddLog(1, "角色ID：" + i);
        }

        Response.Redirect("role_add.aspx");
    }

    /// <summary>
    ///  获取选中权限
    /// </summary>
    /// <returns></returns>
    private string getPermission()
    {
        string PermissionID = "";
        string _PermissionID = Request.Form["chk_ids"];
        if (!string.IsNullOrEmpty(_PermissionID))
        {
            Hashtable para = new Hashtable();
            foreach (string item in _PermissionID.Split(','))
            {
                if (para.ContainsKey(item.Split('_')[0]))
                {
                    para[item.Split('_')[0]] = Convert.ToInt64(para[item.Split('_')[0]]) + int.Parse(item.Split('_')[1]);
                }
                else
                {
                    para[item.Split('_')[0]] = int.Parse(item.Split('_')[1]);
                }
            }

            foreach (string key in para.Keys)
            {
                PermissionID += key + "_" + para[key] + ",";
            }
            PermissionID = System.Text.RegularExpressions.Regex.Replace(PermissionID, ",$", "");
        }

        return PermissionID;
    }

    /// <summary>
    /// 另存为
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void but_assave_Click(object sender, EventArgs e)
    {
        id = 0;
        save();
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void link_del_Click(object sender, EventArgs e)
    {
        if (id < 1)
        {
            return;
        }

        if (rolebll.Delete(id))
        {
            base.AddLog(3, "角色ID：" + id);
            Response.Redirect("role_add.aspx");
        }
    }
}
